package cn.net.yugu.doraemon.datahub.core;

import cn.net.yugu.doraemon.datahub.uitls.BeanUtil;
import com.aliyun.datahub.client.model.Field;
import com.aliyun.datahub.client.model.RecordEntry;
import com.aliyun.datahub.client.model.TupleRecordData;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: wanghongli
 * @create: 2022-07-17 11:06:50
 */
@Data
@AllArgsConstructor
public class RecordConverter {
    private List<Field> fields;
    private Class<?> msgClazz;

    public Object convert2Object(RecordEntry recordEntry) {
        TupleRecordData recordData = (TupleRecordData) recordEntry.getRecordData();
        Map<String, Object> dataMap = new HashMap<>();
        for (Field field : fields) {
            Object obj = recordData.getField(field.getName());
            dataMap.put(field.getName(), obj);
        }
        return BeanUtil.map2Bean(dataMap, msgClazz);
    }
}
